﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using MediaAssistant.Data;
using MediaAssistant.Management;
using MefBasic;
using MefBasic.Commans;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Constants;

namespace MediaAssistant.Controls.Library
{
    [Export(typeof(LibraryPresenter))]
    public class LibraryPresenter:APresenter<ILibraryView>
    {
        [ImportingConstructor]
        public LibraryPresenter(ILibraryView view)
            : base(view)
        {
            ToggleExpandCollapseCommand = new DelegateCommand(ExecuteToggleExpandCollapse);
        }

        public DelegateCommand ToggleExpandCollapseCommand { get; set; }

        private void ExecuteToggleExpandCollapse(object obj)
        {
            WaitScreenService.Show();
            DataSource.RefreshOrderedLibraryItems();
            WaitScreenService.Hide();
        }


        [Import]
        private IWaitScreenService WaitScreenService { get; set; }

        [Import]
        public  LibraryDataSource DataSource
        {
            get { return (LibraryDataSource)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }


        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(LibraryDataSource), typeof(LibraryPresenter), new UIPropertyMetadata(null));

        [Import]
        public Lazy<LibraryManager> LibraryManager { get; set; }

        public void OnItemDoubleClicked(LibraryItem selectedItem)
        {
            if (LibraryItemType.IsMusicItem(selectedItem.Type))
            {
                LibraryManager.Value.ActivateLastResultTypeViews(LibraryItemType.MusicType);
                DataSource.SelectedLibraryItem = selectedItem;
                DataSource.UpdateDuration();
            }
            else if (LibraryItemType.IsMovieItem(selectedItem.Type))
            {
                LibraryManager.Value.ActivateLastResultTypeViews(LibraryItemType.MovieType);
                DataSource.SelectedLibraryItem = selectedItem;
            }
        }
    }
}
